// Copyright@ChenChao


#include "Characters/RPG_CharacterBase.h"

#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GroomComponent.h"
#include "Items/Weapon/Weapon.h"
#include "Animation/AnimInstance.h"
#include "Animation/AnimMontage.h"

ARPG_CharacterBase::ARPG_CharacterBase()
{
	PrimaryActorTick.bCanEverTick = true;

	// 角色运动设置
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	GetCharacterMovement()->bOrientRotationToMovement = true;		//旋转朝向运动
	GetCharacterMovement()->RotationRate = FRotator(0.f, 400.f, 0.f);		//旋转速率
	
	// 弹簧臂
	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
	SpringArm->SetupAttachment(GetRootComponent()); 
	SpringArm->TargetArmLength = 300.f;			//目标臂长
	SpringArm->bUsePawnControlRotation = true;		//使用Pawn控制旋转
	SpringArm->bInheritYaw = true;
	SpringArm->bInheritPitch = true;
	SpringArm->bInheritRoll = true;
	// 摄像机
	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
	Camera->SetupAttachment(SpringArm);

	// 头发
	Hair = CreateDefaultSubobject<UGroomComponent>(TEXT("Hair"));
	Hair->SetupAttachment(GetMesh());
	Hair->AttachmentName = FString("head");
	
	// 眉毛
	Eyebrows = CreateDefaultSubobject<UGroomComponent>(TEXT("Eyebrows"));
	Eyebrows->SetupAttachment(GetMesh());
	Eyebrows->AttachmentName = FString("head");
	
}

void ARPG_CharacterBase::BeginPlay()
{
	Super::BeginPlay();
}

void ARPG_CharacterBase::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ARPG_CharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	// 轴映射
	PlayerInputComponent->BindAxis(FName("MoveForward"), this , &ARPG_CharacterBase::F_MoveForward);
	PlayerInputComponent->BindAxis(FName("MoveRight"), this , &ARPG_CharacterBase::F_MoveRight);
	PlayerInputComponent->BindAxis(FName("Turn"), this , &ARPG_CharacterBase::F_Turn);
	PlayerInputComponent->BindAxis(FName("LookUp"), this , &ARPG_CharacterBase::F_LookUp);
	// 事件映射
	PlayerInputComponent->BindAction(FName("Jump"), EInputEvent::IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction(FName("Equip"), EInputEvent::IE_Pressed, this, &ARPG_CharacterBase::F_Equip);
	PlayerInputComponent->BindAction(FName("Attack"), EInputEvent::IE_Pressed, this, &ARPG_CharacterBase::F_Attack);
}


void ARPG_CharacterBase::F_MoveForward(const float Value)
{
	//如果攻击,就不允许前进
	if (ActionState == ECharacterAction::ECA_Attacking) return;
	if ((Controller != nullptr) && (Value != 0.f))
	{
		const FRotator ControlRotation = GetControlRotation();
		const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);
		//方向:旋转矩阵->获取单位轴向量
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}	
}

void ARPG_CharacterBase::F_MoveRight(const float Value)
{
	//如果攻击,就不允许移动
	if (ActionState == ECharacterAction::ECA_Attacking) return;
	if ((Controller != nullptr) && (Value != 0.f))
	{
		const FRotator ControlRotation = GetControlRotation();
		const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction, Value);
	}	
}

void ARPG_CharacterBase::F_Turn(const float Value)
{
	if ((Controller != nullptr) && (Value != 0.f))
	{
		AddControllerYawInput(Value);
	}
}

void ARPG_CharacterBase::F_LookUp(const float Value)
{
	if ((Controller != nullptr) && (Value != 0.f))
	{
		AddControllerPitchInput(Value);
	}
}

// 捡起装备
void ARPG_CharacterBase::F_Equip()
{
	AWeapon* OverlapWeapon = Cast<AWeapon>(OverlappingItem);
	if (OverlapWeapon)
	{
		//添加武器
		OverlapWeapon->F_Equip(this->GetMesh(), FName("RightHandSocket"));
		CharacterState = ECharacterState::ECS_EquippedOneHandWeapon;
	}
}

// 攻击动作
void ARPG_CharacterBase::F_Attack()
{
	//确定没有攻击进行中
	if (ActionState == ECharacterAction::ECA_Unoccupied)
	{
		if (this->GetCharacterState() == ECharacterState::ECS_UnEquipped)
		{//没有武器
			ActionState = ECharacterAction::ECA_Unoccupied;
		}else if ( this->GetCharacterState() == ECharacterState::ECS_EquippedOneHandWeapon)
		{//装备单手剑
			F_PlayAttackMontage();
			ActionState = ECharacterAction::ECA_Attacking;
		}
	}
}

//播放攻击蒙太奇
void ARPG_CharacterBase::F_PlayAttackMontage()
{
	//获取动画实例
	UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
	//蒙太奇播放
	if (AnimInstance && AttackMontage)
	{	
		//攻击动画蒙太奇
		AnimInstance->Montage_Play(AttackMontage);
		// 设置一个随机数
		uint32 Selection = FMath::RandRange(0,1);
		FName SectionName = FName();
		switch (Selection)
		{
		case 0:
			//从片段1开始播放
				SectionName = FName("Attack1");
			break;
		case 1:
			//从片段2开始播放
				SectionName = FName("Attack2");
			break;
		default:
			break;
		}
		AnimInstance->Montage_JumpToSection(SectionName, AttackMontage);
	}
}

//攻击蒙太奇结束函数
void ARPG_CharacterBase::F_AttackMontageEnd()
{
	ActionState = ECharacterAction::ECA_Unoccupied;
}


